<?php
/*
+--------------------------------------------------------------------------
|   phpBIZ v3.0  full version
|   ========================================
|   by taft@wjl.cn yejun@wjl.cn
|   http://www.phpbiz.cn http://www.wjl.cn
|   all rights reserved
+---------------------------------------------------------------------------
|
|   > 最后修改日期：2005-4-30 2006-2-8
|
+--------------------------------------------------------------------------
*/
!class_exists('Debug') && exit('Forbidden');
$idx = new Order;
class Order
{

	function Order()
	{
		global $INCOME,$BIZ;

		if ( empty($BIZ->seller['seller_id']) or $BIZ->seller['seller_id'] == "" or $BIZ->seller['seller_id'] == 0 )
		{
			$BIZ->error( "用户没有登陆，不能使用此功能","?act=Login&code=00" );
		}

		$user_id = $BIZ->seller['seller_id'];
		
		define("PAGE_NUM",10);
		$BIZ -> assign("code",$INCOME['code']);
	    $BIZ -> nav_link[] = array("商家控制台","index.php?act=Seller");
	    switch($INCOME['code'])
		{
			case 'add':
				$BIZ -> assign( "list_nav","下定单");
				$this -> add_order();
			    break;
			case 'doadd':
				$this -> do_add();
			    break;
		    //-----------------------------
			case 'view1':
				$this ->_check_use($user_id);
			    $BIZ -> nav_link[] = array("未处理订单");
				$this -> view_order(0);  //商家查看未处理订单
			    break;
			case 'view2':
				$this ->_check_use($user_id);
			    $BIZ -> nav_link[] = array("已处理订单");
				$this -> view_order(1);  //商家查看已处理订单
			    break;
			case 'delo':                //删除订单
			    $this ->_check_use($user_id);
				$this -> del_order();
			    break;
		    case 'process':
				$this ->_check_use($user_id);
				$this -> process_form();   //处理表单
			    break;
			case 'doprocess':
				$this ->_check_use($user_id);
				$this -> process_form(1);  //处理
			    break;
			//-----------------------------
			case 'uview':
				$BIZ -> nav_link[] = array("查看订单记录");
				$this -> view_myorder();  //买家查看
			    break;			    

		}
	}

	function view_order($process)
	{
		global $INCOME,$BIZ,$DB;

    	//------------------------------------
		//paging
		//------------------------------------
		$start = $INCOME['st'] ? intval( $INCOME['st'] ) : 0;

		$DB -> db_query( "SELECT count(*) as count FROM biz_order WHERE sid=".$BIZ->seller[seller_id]." AND process=".$process );
		
		$row_count = $DB -> db_fetch_row();
		$pages = $BIZ->build_pagelinks( array('TOTAL_POSS'  => $row_count['count'],
											   'PER_PAGE'    => PAGE_NUM,
											   'CUR_ST_VAL'  => $start,
											   'L_SINGLE'    => "一页",
											   'L_MULTI'     => "页：",
											   'BASE_URL'    => $this->base_url."?act=order"."&code={$INCOME['code']}"
											   )
											);
		//--------------------------------------

		$query = " SELECT bg.goods_name, bo.* FROM biz_goods bg JOIN biz_order bo ON bo.gid=bg.goods_id WHERE sid=".$BIZ->seller[seller_id]." AND bo.process=".$process." ORDER BY time DESC LIMIT $start,".PAGE_NUM;

		$DB -> db_query( $query );

		while($r = $DB -> db_fetch_row() )
		{
			$tm = date("y-m-d H:i",$r['time']);
			$olist[] = array($r['id'],$r['goods_name'],$r['quantity'],$tm);
		}

		$BIZ->assign("olist",$olist);
		$BIZ->assign("plink",$pages);
		$BIZ->output("Order.tpl");

	
	}
	function view_myorder()
	{
		global $BIZ,$DB,$INCOME;

		//------------------------------------
		//paging
		//------------------------------------
		$start = $INCOME['st'] ? intval( $INCOME['st'] ) : 0;

		$DB -> db_query( "SELECT count(*) as count FROM biz_order WHERE bid=".$BIZ->seller[seller_id]);
		$row_count = $DB -> db_fetch_row();
		$pages = $BIZ->build_pagelinks( array('TOTAL_POSS'  => $row_count['count'],
											   'PER_PAGE'    => PAGE_NUM,
											   'CUR_ST_VAL'  => $start,
											   'L_SINGLE'    => "一页",
											   'L_MULTI'     => "页：",
											   'BASE_URL'    => $this->base_url."?act=order"."&code={$INCOME['code']}"
											   )
											);
    	$query = " SELECT bg.goods_name, bo.* FROM biz_goods bg JOIN biz_order bo ON bo.gid=bg.goods_id WHERE bid=".$BIZ->seller[seller_id]."  ORDER BY time DESC LIMIT $start,".PAGE_NUM;

		$DB -> db_query( $query );
		while($r = $DB -> db_fetch_row() )
		{
			$tm = date("y-m-d H:i",$r['time']);
			$status = $r['process']?"已受理":"未受理";
			$olist[] = array($r['id'],$r['goods_name'],$r['quantity'],$tm,$status,$r['process']);
		}

		$BIZ->assign("olist",$olist);
		$BIZ->assign("plink",$pages);
     	$BIZ->output("Order.tpl");

	}
	function add_order()
	{
		global $INCOME,$BIZ,$DB;

	    $id = intval( $INCOME['GID'] );

	   //--------------------------------------
	   //check if exsit
	   //--------------------------------------
        $DB -> db_query( "SELECT count(*) as ct FROM biz_goods WHERE goods_id='".$id."'" );
		$row = $DB -> db_fetch_row();
		if( $row['ct'] !=1 ) $BIZ -> error( "商品查询错误","?act=Lst&code=g" );
	   //--------------------------------------

	   $DB  -> db_query( "SELECT g.*, p.pic_name,s.*,b.seller_logaccount,b.seller_qq_number,b.seller_logaccount,b.seller_id FROM biz_goods g, biz_pic p,biz_shop s, biz_seller b WHERE goods_id='".$id."' AND g.goods_mark_sell='0' AND p.pic_id = g.goods_prime_picture_id AND s.shop_id = g.shop_id  AND b.seller_id = g.seller_id");

	   $g = $DB -> db_fetch_row();
	   $this ->_check_use($g['seller_id']);  //查看该商家是否有订单功能。	   
       $g['shop_describle'] = nl2br($g['shop_describle']);
       $g['goods_store_date'] = date("Y-m-d",$g['goods_store_date']); 
	   $g['pic_name']=$BIZ -> make_full_path($g['pic_name']);
       
	   $BIZ -> nav_link[] = array("查看商品","");
	   $BIZ -> assign("glist",$g);
	   $BIZ -> assign("bname",$BIZ -> seller[seller_logaccount]);
	   $BIZ -> assign("telephone",$BIZ -> seller[seller_telephone]);
	   $BIZ -> output("GoodsOrder.tpl");
	}
	function do_add()
	{
		global $INCOME,$BIZ,$DB;

	    $id = intval( $INCOME['GID'] );
		$od = $INCOME['od'];
		$od['time'] = time();
		$od['bid'] = $BIZ -> seller['seller_id'];
		$DB -> db_form_insert("biz_order",$od);
		$BIZ ->redirect_screen("恭喜！您已经成功地下了一个订单，该商家会立即处理。","?act=order&code=uview");

	}
	function del_order()
	{
		global $INCOME,$BIZ,$DB;
		$id_data= @implode(",",$INCOME[delo]);
		if($id_data)
		{
		$DB -> db_query("DELETE FROM biz_order WHERE id IN (".$id_data.")");
		$BIZ ->redirect_screen("恭喜！您已经成功删除了订单记录","?act=order&code={$INCOME[precode]}");	
		}else
			$BIZ -> error("错误：未选定删除记录");
	}
	function process_form($flag=0)
	{
		global $INCOME,$BIZ,$DB;
		
		$id = intval($INCOME[OID]);
		if($flag)
		{
			$p = $INCOME['finish']?1:0;
			$DB ->db_query(" UPDATE biz_order SET process=".$p." WHERE id=".$id);
			$BIZ ->redirect_screen("恭喜！您已经成功受理了一条订单记录","?act=order&code=view1");	

		}else
		{
			$DB -> db_query(" SELECT bo.*,bg.goods_name FROM biz_order bo JOIN biz_goods bg ON bg.goods_id=bo.gid WHERE id=".$id." AND sid=".$BIZ->seller['seller_id']);
			$BIZ -> assign("order",$DB -> db_fetch_row());
			$BIZ -> output("Order.tpl");
		}
	}
	function _check_use($id)
	{
		global $BIZ,$INCOME;
		$seller_data = new manager($id);
	
		if( !$seller_data ->check($INCOME[act]) )                 
			$BIZ -> error("该等级商家暂时没有开通此功能。");
	}

}
?>